package com.itheima.putige.putige.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.itheima.putige.putige.common.BaseContext;
import com.itheima.putige.putige.common.R;
import com.itheima.putige.putige.entity.AddressBook;
import com.itheima.putige.putige.service.AddressBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author XGC
 * @description: TODO 地址
 * @date 2022/03/01 20:48
 * @title: AddressBookController
 * @projectName ruiji
 */
@RestController
@RequestMapping("/addressBook")
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;

    /**
     * @Author XGC
     * @Description //TODO 查询所有地址
     * @Date 20:55 2022/03/01
     * @Param [session]
     * @return com.itheima.putige.putige.common.R<java.util.List<com.itheima.putige.putige.entity.AddressBook>>
     */
    @GetMapping("/list")
    public R<List<AddressBook>> list(HttpSession session){
        //获取当前登录用户Id
        Long userId = (Long) session.getAttribute("user");
        LambdaQueryWrapper<AddressBook> lqw = new LambdaQueryWrapper<>();
        //添加条件
        lqw.eq(AddressBook::getUserId,userId);
        lqw.orderByDesc(AddressBook::getUpdateTime);
        //查询地址
        List<AddressBook> list = addressBookService.list(lqw);
        return R.success(list);
    }

    /**
     * @Author XGC
     * @Description //TODO 添加地址
     * @Date 21:01 2022/03/01
     * @Param [addressBook]
     * @return com.itheima.putige.putige.common.R<java.lang.String>
     */
    @PostMapping
    public R<String> addAddress(@RequestBody AddressBook addressBook){
        //获取当前登录用户Id
        Long userId = BaseContext.getCurrentId();

        addressBook.setUserId(userId);

        addressBookService.save(addressBook);
        return R.success("添加地址成功！");
    }

    /**
     * @Author XGC
     * @Description //TODO 设置默认地址
     * @Date 21:08 2022/03/01
     * @Param [addressBook]
     * @return com.itheima.putige.putige.common.R<java.lang.String>
     */
    @PutMapping("/default")
    public R<String> setDefaultAddress(@RequestBody AddressBook addressBook){
        //获取当前登录用户Id
        Long userId = BaseContext.getCurrentId();
        //先将该用户所有地址的is_default更新为0
        LambdaUpdateWrapper<AddressBook> luw = new LambdaUpdateWrapper<>();
        luw.eq(AddressBook::getUserId,userId);
        luw.set(AddressBook::getIsDefault,0);
        addressBookService.update(luw);

        //然后将当前的设置的默认地址的is_default设置为1
        addressBook.setIsDefault(1);
        addressBookService.updateById(addressBook);

        return R.success("设置默认地址成功！");
    }

    /**
     * @Author XGC
     * @Description //TODO 查询单个地址
     * @Date 21:15 2022/03/01
     * @Param [id]
     * @return com.itheima.putige.putige.common.R<com.itheima.putige.putige.entity.AddressBook>
     */
    @GetMapping("/{id}")
    public R<AddressBook> addressFindOne(@PathVariable Long id){
        LambdaQueryWrapper<AddressBook> lqw = new LambdaQueryWrapper<>();
        lqw.eq(AddressBook::getId,id);
        AddressBook addressBook = addressBookService.getOne(lqw);
        if(addressBook != null){
            return R.success(addressBook);
        }
        return R.error("没找到该对象");
    }

    /**
     * @Author XGC
     * @Description //TODO 修改地址
     * @Date 21:25 2022/03/01
     * @Param [addressBook]
     * @return com.itheima.putige.putige.common.R<java.lang.String>
     */
    @PutMapping
    public R<String> updateAddress(@RequestBody AddressBook addressBook){
        addressBookService.updateById(addressBook);
        return R.success("修改成功!");
    }

    /**
     * @Author XGC
     * @Description //TODO 删除地址
     * @Date 21:35 2022/03/01
     * @Param [ids]
     * @return com.itheima.putige.putige.common.R<java.lang.String>
     */
    @DeleteMapping
    public R<String> deleteAddress(@RequestParam List<Long> ids){

        addressBookService.removeByIds(ids);
        return R.success("删除地址成功！");
    }

    /**
     * @Author XGC
     * @Description //TODO 查询默认地址
     * @Date 21:38 2022/03/01
     * @Param []
     * @return com.itheima.putige.putige.common.R
     */
    @GetMapping("/default")
    public R getDefaultAddress(){
        LambdaQueryWrapper<AddressBook> lqw = new LambdaQueryWrapper<>();
        lqw.eq(AddressBook::getUserId,BaseContext.getCurrentId());
        lqw.eq(AddressBook::getIsDefault,1);

        AddressBook addressBook = addressBookService.getOne(lqw);
        if(addressBook != null){
            return R.success(addressBook);
        }
        return R.error("您还未设定默认地址！");
    }

}
